package com.clouddong.exterapi.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReqHttpClientUtil{
//  //用户认证(UserName、Password)
//    public static CloseableHttpClient getHttpClient(){
//            CredentialsProvider provider = new BasicCredentialsProvider();
//            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("User", "Password");
//            provider.setCredentials(AuthScope.ANY, credentials);
//            return  HttpClients.custom().setDefaultCredentialsProvider(provider).build();
//        }
//    public static String  FyCall(String url,String Input) {
//            String s = "";
//            CloseableHttpClient httpClient = getHttpClient();
//            HttpPost httpPost = new HttpPost(url);
//            httpPost.setHeader("Content-Type", "application/json");
//            httpPost.setHeader("Accept", "application/json");
//            StringEntity strentity;
//            try {
//                strentity = new StringEntity(Input,"UTF-8");
//               httpPost.setEntity(strentity);
//               CloseableHttpResponse response = httpClient.execute(httpPost);
//                HttpEntity entity =response.getEntity();
//                s = EntityUtils.toString(entity);
//            } catch (Exception e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//            return s;
//        }
//    
    
    private final static Logger logger=LoggerFactory.getLogger(ReqHttpClientUtil.class);
    
    public  static String  FyCall(String urlParam,String Input) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(urlParam);
        httpPost.addHeader("Content-Type","application/json");
        int CONNECTION_TIMEOUT = 10000;
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build();
        String str = null;
        try{
            httpPost.setConfig(requestConfig);
            String jsonparam = Input;
            StringEntity stringEntity = new StringEntity(jsonparam,Charset.forName("UTF-8"));
            httpPost.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(httpPost);
            //读取服务器响应数据
            BufferedReader bufferedReader=null;
            
            if (response.getStatusLine().getStatusCode() == 200){
                bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                str = bufferedReader.readLine();
                logger.info("返回结果："+str);
                System.out.println("返回结果："+str);
            }else{
                logger.info("请求失败:"+response.getStatusLine().getStatusCode());
                System.out.println("请求失败:"+response.getStatusLine().getStatusCode());
            }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
            try{
                httpClient.close();
            }catch (IOException e){
                throw new RuntimeException(e);
            }
        }
        return str;
   }
}
